package com.beiming.libra.controller;

import com.beiming.libra.common.enums.CaseUserTypeEnum;
import com.beiming.libra.common.enums.ErrorCode;
import com.beiming.libra.domain.ArbitrationCaseProof;
import com.beiming.libra.domain.dto.requestdto.ArbitrationCaseProofCheckRequestDTO;
import com.beiming.libra.domain.dto.requestdto.ArbitrationCaseProofRequestDTO;
import com.beiming.libra.domain.dto.responsedto.CaseProofResponseDTO;
import com.beiming.libra.domain.dto.responsedto.CaseUserTypeResponseDto;
import com.beiming.libra.helper.Result;
import com.beiming.libra.service.ArbitrationCaseProofService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @title 举证质证接口
 * @author zhengyu
 * @create_time 2018/6/1
 */
@RestController
@Api(value = "举证质证接口", tags = "举证质证接口")
@RequestMapping("/api/arbitrationCaseProof")
public class ArbitrationCaseProofController {

  @Resource
  private ArbitrationCaseProofService arbitrationCaseProofService;

  @GetMapping("/{caseId}/{status}")
  @ApiOperation(value = "获取举证质证信息", notes = "获取举证质证信息")
  public Result<List<CaseProofResponseDTO>> listArbitrationCaseProofs(@ApiParam(value = "仲裁申请表id", required = true) @PathVariable String caseId,
      @ApiParam(value = "数据状态0(正常)2(申请补正待审核)3(申请补正被退回)5(返回所有状态数据)", required = true) @PathVariable String status) {
    if (StringUtils.isEmpty(caseId)) {
      return Result.failed(ErrorCode.ILLEGAL_PARAMETER, "caseId参数必传");
    }
    List<CaseProofResponseDTO> arbitrationCaseProofDTOs = arbitrationCaseProofService.listArbitrationCaseProofs(caseId, status);
    return Result.success(arbitrationCaseProofDTOs);
  }

  @PostMapping("")
  @ApiOperation(value = "保存举证信息", notes = "保存举证质证信息")
  public Result createArbitrationCaseProof(@RequestBody ArbitrationCaseProofRequestDTO arbitrationCaseProofRequestDTO) {
    //批量保存
    try {
      arbitrationCaseProofService.createArbitrationCaseProof(arbitrationCaseProofRequestDTO);
    } catch (RuntimeException e) {
      return  Result.failed(ErrorCode.UNEXCEPTED, e.getMessage());
    }

    return Result.success();
  }

  @PostMapping("preProof")
  @ApiOperation(value = "保存待审核举证信息", notes = "保存待审核举证信息")
  public Result createPreArbitrationCaseProof(@RequestBody ArbitrationCaseProofRequestDTO arbitrationCaseProofRequestDTO) {
    //批量保存
    try {
      arbitrationCaseProofService.createPreArbitrationCaseProof(arbitrationCaseProofRequestDTO);
    } catch (RuntimeException e) {
      return  Result.failed(ErrorCode.UNEXCEPTED, e.getMessage());
    }

    return Result.success();
  }

  @PutMapping("")
  @ApiOperation(value = "保存质证信息", notes = "保存举证质证信息")
  public Result updateArbitrationCaseProof(@RequestBody ArbitrationCaseProofCheckRequestDTO arbitrationCaseProofCheckRequestDTO) {
    //批量保存
    try {
      arbitrationCaseProofService.updatgeArbitrationCaseProof(arbitrationCaseProofCheckRequestDTO);
    } catch (RuntimeException e) {
      return  Result.failed(ErrorCode.UNEXCEPTED, e.getMessage());
    }

    return Result.success();
  }

  @PutMapping("/status")
  @ApiOperation(value = "审核超期举证信息", notes = "审核超期举证信息")
  public Result checkArbitrationCaseProof(@ApiParam(value = "举证质证表id", required = true) String id,
      @ApiParam(value = "审核状态0(不通过)1(通过)", required = true) String status) {
    //获取对应审核通过和不通过的状态值
    status = ArbitrationCaseProof.getCheckStatus(status);
    arbitrationCaseProofService.updateStatus(id, status);
    return Result.success();
  }

  @GetMapping("/caseUserTypes")
  @ApiOperation(value = "获取案件角色全数据", notes = "获取案件角色全数据")
  public Result<List<CaseUserTypeResponseDto>> getCaseUserTypes() {
    List<CaseUserTypeResponseDto> caseUserTypeResponseDtos = new ArrayList<CaseUserTypeResponseDto>();
    for (CaseUserTypeEnum caseUserTypeEnum : CaseUserTypeEnum.values()) {
      CaseUserTypeResponseDto caseUserTypeResponseDto = new CaseUserTypeResponseDto();
      caseUserTypeResponseDto.setCode(caseUserTypeEnum.getCode());
      caseUserTypeResponseDto.setName(caseUserTypeEnum.getName());
      caseUserTypeResponseDtos.add(caseUserTypeResponseDto);
    }
    return Result.success(caseUserTypeResponseDtos);
  }
}
